SetGfxDriver 1:Graphics 1024,768,8,2:SeedRnd MilliSecs() :SetBuffer FrontBuffer() 
Global gmode,dump1,in1,in2,in3,in9,ztx,sort1,zy1,unittestcnt,zzz1,zzz2,null1,facts,factz,facts2,factz2,facts3,factz3,facts4,factz4,infacts4,infactz4,modfacts1,z,modfacts2,testinfacts1,testinfacts2,refactorfrom,refactorto,refactorrule,refactorruleold,yy
in1 = CreateBank(20000):in2 = CreateBank(20000):in3 = CreateBank(20000):in9 = CreateBank(20000):sort1 = CreateBank(60):facts=CreateBank(125):factz=CreateBank(125):factz2=CreateBank(125):facts2=CreateBank(125):factz3=CreateBank(125):facts3=CreateBank(125):factz4=CreateBank(125):facts4=CreateBank(125):infactz4=CreateBank(125):infacts4=CreateBank(125)
modfacts1=CreateBank(125):modfacts2=CreateBank(125):testinfacts1=CreateBank(125):testinfacts2=CreateBank(125):refactorfrom=CreateBank(125):refactorto=CreateBank(125)
dump1 = CreateBank(250)
Dim decode(1000000):Dim aaa(60):Dim bbb(10,10)
Dim encode(1000000)
decodet()
Dim zzz(10000)
Dim z1zz(10000)
Dim err(10000)
;Include "gui.bb"



While Not KeyHit(1) 
ClsColor 0,0,0
Cls

randnum()

getfact3()
Color 255,255,0:Text 600,15,ztx,False ,False 
CopyBank in1,0,in2,0,20000
morf1()


CopyBank in2,0,in3,0,20000
demorf1()

demorf2(ztx):dumpbank():WaitMouse()
For i=0 To zzz1
If i*10 > 1000 Then dy=Int(i*10/1000)
	Color 255,255,0:Text (i*30) Mod 1000,300+dy*15,Str(zzz(i))+"/"+Str(z1zz(i)) +",",False ,False 
Next
Flip
;WaitMouse() 
VWait 
Wend 

Function testinfacts(ztl)
	For y=0 To 3
	For x=0 To 3
	PokeInt sort1,x*5,PeekInt(infactz4,16*y+(x*5))
	Next
        sortrow()
	For x=0 To 3
	PokeInt infactz4,5*(y*4+x),PeekByte (sort1,5*x)
	Next
	ttt = Int(""+ PeekByte (sort1,0*5) +""+PeekByte (sort1,1*5) +""+PeekByte (sort1,2*5)+""+PeekByte (sort1,3*5))
	xx = encode(ttt*5)
	PokeInt infactz4,y*5,xx	
;	Color 0,0,255:Text 120,y*9+470,xx,False ,False 	
	Next

	For y=0 To 3
	PokeInt sort1,y*5,PeekInt(infactz4,5*y)
	Next
        sortrow()
	For x=0 To 3
	PokeInt infactz4,y*5,PeekByte (sort1,5*y)
	Next

	ttt = Int(""+ PeekByte (sort1,0*5) +""+PeekByte (sort1,1*5) +""+PeekByte (sort1,2*5)+""+PeekByte (sort1,3*5))
	xx = encode(ttt*5)
	PokeInt infactz4,y*5,xx:zy1=xx
	If (ztl =xx) null1=null1+1:zzz(zzz1)=zzz(zzz1)+1:z1zz(zzz1)=xx;:dumpbank():WaitMouse()

End Function

Function getinfacts3()
	For y=0 To 3
	For x=0 To 3
	PokeInt sort1,x*5,PeekInt(in3,4*(y*4+x))
	Next
        sortrow()
	ttt = Int(""+ PeekByte (sort1,0*5) +""+PeekByte (sort1,1*5) +""+PeekByte (sort1,2*5)+""+PeekByte (sort1,3*5))
	xx = encode(ttt*5)
	PokeInt infactz4,y*5,xx	
	Next
	
	For y=0 To 3
	For x=0 To 3
	PokeInt sort1,x*5,PeekInt(in3,16*5+5*(y*4+x))
	Next
        sortrow()
	ttt = Int(""+ PeekByte (sort1,0*5) +""+PeekByte (sort1,1*5) +""+PeekByte (sort1,2*5)+""+PeekByte (sort1,3*5))
	xx = encode(ttt*5)
	PokeInt infactz4,16+y*5,xx	
	Next

	
	For y=0 To 3
	For x=0 To 3
	PokeInt sort1,x*5,PeekInt(in3,32*5+5*(y*4+x))
	Next
        sortrow()
	ttt = Int(""+ PeekByte (sort1,0*5) +""+PeekByte (sort1,1*5) +""+PeekByte (sort1,2*5)+""+PeekByte (sort1,3*5))
	xx = encode(ttt*5)
	PokeInt infactz4,32+y*5,xx	
	Next
	
	For y=0 To 3
	For x=0 To 3
	PokeInt sort1,x*5,PeekInt(in3,48*5+5*(y*4+x))
	Next
        sortrow()
	ttt = Int(""+ PeekByte (sort1,0*5) +""+PeekByte (sort1,1*5) +""+PeekByte (sort1,2*5)+""+PeekByte (sort1,3*5))
	xx = encode(ttt*5)
	PokeInt infactz4,48+y*5,xx	
	Next
	
;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
	
	For y=0 To 3
	For x=0 To 3
	PokeInt sort1,x*5,PeekInt(infactz4,16*y+(x*5))
	Next
        sortrow()
	ttt = Int(""+ PeekByte (sort1,0*5) +""+PeekByte (sort1,1*5) +""+PeekByte (sort1,2*5)+""+PeekByte (sort1,3*5))
	xx = encode(ttt*5)
	PokeInt factz4,y*5,xx	
	Next

	For y=0 To 3
	PokeInt sort1,y*5,PeekInt(factz4,5*y)
	Next
        sortrow()
	ttt = Int(""+ PeekByte (sort1,0*5) +""+PeekByte (sort1,1*5) +""+PeekByte (sort1,2*5)+""+PeekByte (sort1,3*5))
	xx = encode(ttt*5)
	PokeInt factz3,y*5,xx:z=xx:ztx=xx

End Function


Function mod1(x)
While x>255 
 x = x -256
Wend 
While  x<0 
 x = x +256
Wend 
Return x
End Function
Function mod2(x)
If x>65535 Then x = x -65536
;Wend 
If  x<0 Then x = x +65536
;Wend 
Return x
End Function

Function demorf2(ert)
CopyBank in9,0,in3,0,20000
For zt1=ert To 1 Step -1
For y1=1 To 64
x = PeekInt (in3,(y1-1)*5)
x1 = Int(Floor(x/256))
x2 = x-x1*256
x2 =mod1(x2+zt1+y1*10); -ztx-zt1*10-y1)
x1 =mod1(x1-zt1-y1*10); -ztx-zt1*10-y1)
x = x1*256+x2
PokeInt in3,(y1-1)*5,x
Next
Next
End Function


Function demorf1()
CopyBank in3,0,in9,0,20000
For ztl=1 To 24
CopyBank in9,0,in3,0,20000
For zt1=ztl To 1 Step -1
For y1=1 To 64
x = PeekInt (in3,(y1-1)*5)
x1 = Int(Floor(x/256))
x2 = x-x1*256
x2 =mod1(x2+zt1+y1*10); -ztx-zt1*10-y1)
x1 =mod1(x1-zt1-y1*10); -ztx-zt1*10-y1)
x = x1*256+x2
PokeInt in3,(y1-1)*5,x
Next
Next
getinfacts3()
testinfacts(ztl)
Next
zzz1=zzz1+1
End Function

Function morf1() 
For zt1=1 To ztx 
For y1=1 To 64
x = PeekInt (in2,(y1-1)*5)
x1 = Int(Floor(x/256))
x2 = x-x1*256
x1 =mod1(x1+zt1+y1*10); +ztx+zt1*10+y1)
x2 =mod1(x2-zt1-y1*10); +ztx+zt1*10+y1)
x = x1*256+x2
PokeInt in2,(y1-1)*5,x
Next
Next
End Function




Function getfact3()
	For y=0 To 3
	For x=0 To 3
	PokeInt sort1,x*5,PeekInt(in1,5*(y*4+x))
	Next
        sortrow()
	ttt = Int(""+ PeekByte (sort1,0*5) +""+PeekByte (sort1,1*5) +""+PeekByte (sort1,2*5)+""+PeekByte (sort1,3*5))
	xx = encode(ttt*5)
	PokeInt factz,y*5,xx	
	Next
	
	For y=0 To 3
	For x=0 To 3
	PokeInt sort1,x*5,PeekInt(in1,16*5+5*(y*4+x))
	Next
        sortrow()
	ttt = Int(""+ PeekByte (sort1,0*5) +""+PeekByte (sort1,1*5) +""+PeekByte (sort1,2*5)+""+PeekByte (sort1,3*5))
	xx = encode(ttt*5)
	PokeInt factz,16+y*5,xx	
	Next

	
	For y=0 To 3
	For x=0 To 3
	PokeInt sort1,x*5,PeekInt(in1,32*5+5*(y*4+x))
	Next
        sortrow()
	ttt = Int(""+ PeekByte (sort1,0*5) +""+PeekByte (sort1,1*5) +""+PeekByte (sort1,2*5)+""+PeekByte (sort1,3*5))
	xx = encode(ttt*5)
	PokeInt factz,32+y*5,xx	
	Next
	
	For y=0 To 3
	For x=0 To 3
	PokeInt sort1,x*5,PeekInt(in1,48*5+5*(y*4+x))
	Next
        sortrow()
	ttt = Int(""+ PeekByte (sort1,0*5) +""+PeekByte (sort1,1*5) +""+PeekByte (sort1,2*5)+""+PeekByte (sort1,3*5))
	xx = encode(ttt*5)
	PokeInt factz,48+y*5,xx	
	Next
;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
	For y=0 To 3
	For x=0 To 3
	PokeInt sort1,x*5,PeekInt(factz,16*y+(x*5))
	Next
        sortrow()
	ttt = Int(""+ PeekByte (sort1,0*5) +""+PeekByte (sort1,1*5) +""+PeekByte (sort1,2*5)+""+PeekByte (sort1,3*5))
	xx = encode(ttt*5)
	PokeInt factz2,y*5,xx	
	Next

	For y=0 To 3
	PokeInt sort1,y*5,PeekInt(factz2,5*y)
	Next
        sortrow()
	ttt = Int(""+ PeekByte (sort1,0*5) +""+PeekByte (sort1,1*5) +""+PeekByte (sort1,2*5)+""+PeekByte (sort1,3*5))
	xx = encode(ttt*5)
	PokeInt factz3,y*5,xx:z=xx:ztx=xx
End Function

Function sortrow()

	For xt=0 To 3
		aaa(xt*5)=PeekInt (sort1,xt*5)+1
	Next
	For x=4 To 1 Step -1
	maxx =0
	For xt=0 To 3
	xx = aaa(xt*5):If maxx<xx Then maxx=xx:yy2=xt*5
	Next 
	aaa(yy2) =-10:PokeByte sort1,yy2,x
	Next
End Function 

Function randnum()
For i = 0 To 63 
t = Rand(0,65535):PokeInt in1,i*5, t:PokeInt in2,i*5, t
Next 
End Function

Function dumpbank()
For bb=0 To 64
bb1=PeekInt(in1,bb*5)
dx1 = Int(Floor(bb1/256))
dx2 = bb1-dx1*256
PokeByte dump1,bb*2,dx1
PokeByte dump1,bb*2+1,dx2
Next
fileBank=WriteFile("c:\test1.bnk") 
WriteBytes dump1,fileBank,0,250 
CloseFile fileBank 
FreeBank bnkTest 

For bb=0 To 64
bb1=PeekInt(in3,bb*5)
dx1 = Int(Floor(bb1/256))
dx2 = bb1-dx1*256
PokeByte dump1,bb*2,dx1
PokeByte dump1,bb*2+1,dx2
Next
fileBank=WriteFile("c:\test2.bnk") 
WriteBytes dump1,fileBank,0,250 
CloseFile fileBank 
FreeBank bnkTest 
End Function


Function decodet()
y=1
For x1=1 To 4
For x2=1 To 4
For x3=1 To 4
For x4=1 To 4
If((x1 <> x2) And (x1<>x3) And (x1<>x4) And (x2<>x3) And (x2<>x4) And (x3<>x4)) decode(y*5) =x1+""+x2+""+x3+""+x4:encode(Int(x1+""+x2+""+x3+""+x4)*5)=y:y=y+1
Next
Next
Next
Next
End Function